package vcp.views;

import java.awt.event.KeyEvent;
import java.util.HashMap;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.event.AncestorListener;
import javax.swing.event.ChangeListener;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.util.Rotation;

import client.Message;
import db.parking.beans.SystemDetails;
import vcp.common.AbstractView;
import vcp.common.IView;
import vcp.gui.panels.networkmanager.PnlSystemDetails;

/**
 * System State View
 *
 */
public class SystemStateView extends AbstractView implements IView, Observer {
	PnlSystemDetails pnlSystemDetails;
    boolean systemDetailsLoaded = false;
	public SystemStateView() {
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public void update(Observable arg0, Object arg) {
		// populates graphs tabs
		Message tmpMessage = (Message) arg;
		HashMap<String, Object> hm;
		if (tmpMessage.getStatusMsg().equals("GetSystemState") && systemDetailsLoaded==false) {
			if(tmpMessage.getObj()!=null){
			hm = (HashMap<String, Object>)tmpMessage.getObj();
			SystemDetails data = (SystemDetails) hm.get("Batch_details");
			
			
			JTabbedPane tpane = pnlSystemDetails.getTabbedPane();
			PieDataset dataset = createOrderDetailsDataset(data);
			JFreeChart chart = createChart(dataset, "Orders");
			ChartPanel chartPanel = new ChartPanel(chart);
			chartPanel.setSize(new java.awt.Dimension(500, 270));
			tpane.addTab("Orders", null,  chartPanel, "Orders");
			tpane.setMnemonicAt(0, KeyEvent.VK_1);
			
			
			
			PieDataset dataset2 = createCustomerDetailsDataset(data);
			JFreeChart chart2 = createChart(dataset2, "Customers");
			ChartPanel chartPanel2 = new ChartPanel(chart2);
			chartPanel2.setSize(new java.awt.Dimension(500, 270));
			tpane.addTab("Customers", null, chartPanel2, "Customers");
			tpane.setMnemonicAt(0, KeyEvent.VK_2);
			
            
			int subNum = (int)hm.get("subNum");
			int subNumMoreThanOnCar = (int)hm.get("subNumMoreThanOnCar");
			
			PieDataset dataset3 = subscriptionsSet(subNum,subNumMoreThanOnCar);
			JFreeChart chart3 = createChart(dataset3, "Subscribers");
			ChartPanel chartPanel3 = new ChartPanel(chart3);
			chartPanel3.setSize(new java.awt.Dimension(500, 270));
			tpane.addTab("Subscribers", null, chartPanel3, "Subscribers");
			tpane.setMnemonicAt(0, KeyEvent.VK_3);
			
			
			
			systemDetailsLoaded = true;

	
		  }
		}
	}

	private PieDataset subscriptionsSet(int subNum, int subNumMoreThanOnCar) {
		DefaultPieDataset result = new DefaultPieDataset();		
		result.setValue("Subscribers", subNum);
		result.setValue("Subscribers with more than one car", subNumMoreThanOnCar);

		return result;
	}

	@Override
	public void addController(Object obj) {
		getPnlSystemDetails().getTabbedPane().addChangeListener((ChangeListener)obj);
	}

	public PnlSystemDetails getPnlSystemDetails() {
		if(pnlSystemDetails == null)
		  pnlSystemDetails = new PnlSystemDetails();
		return pnlSystemDetails;
	}

	private PieDataset createOrderDetailsDataset(SystemDetails data) {
		DefaultPieDataset result = new DefaultPieDataset();
		result.setValue("Occasional Orders", data.getOrdersOccasional());
		result.setValue("Temporary Orders", data.getOrdersTemporary());
		result.setValue("Cancelled orders", data.getCancelledOrders());
		result.setValue("Subscriptions", data.getSubscriptions());
		//result.setValue("Customers", data.getCustomersCount());
		//result.setValue("Total cars", data.getCarsAmount());

		return result;
	}
	
	private PieDataset createCustomerDetailsDataset(SystemDetails data) {
		DefaultPieDataset result = new DefaultPieDataset();		
		result.setValue("Customers", data.getCustomersCount());
		result.setValue("Total cars", data.getCarsAmount());

		return result;
	}
	
	


	private JFreeChart createChart(PieDataset dataset, String title) {

		JFreeChart chart = ChartFactory.createPieChart3D(title, // chart title
				dataset, // data
				true, // include legend
				true, false);

		PiePlot3D plot = (PiePlot3D) chart.getPlot();
		plot.setStartAngle(290);
		plot.setDirection(Rotation.CLOCKWISE);
		plot.setForegroundAlpha(0.5f);
		return chart;

	}

}
